package com.wyp.didishige.activity.dynamic;

import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.wyp.didishige.activity.BaseActivity;
import com.wyp.didishige.activity.MainActivity;
import com.wyp.didishige.config.Constants;
import com.wyp.didishige.model.User;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.wiget.xlist.XListView;
import com.wyp.didishige.R;
import com.wyp.didishige.adapter.DiscoverAdapter;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.model.Dynamic;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.listener.FindListener;

public class UserDynamicListActivity extends BaseActivity implements View.OnClickListener, XListView.IXListViewListener {
    BmobGeoPoint geoPoint;
    List<Dynamic> dynamicList;
    private static final String USER = "user";

    XListView nearby_list;
    DiscoverAdapter nearByAdapter;

    private SwipeRefreshLayout hullSwipe = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_dynamic_list);
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("动态列表");
        init();
    }

    @Override
    protected void initParamter() {
        super.initParamter();
        nearByAdapter = new DiscoverAdapter(UserDynamicListActivity.this, new ArrayList<Dynamic>());
        Bundle bundle=getIntent().getExtras();
        if (null!=bundle){
            userId=bundle.getString(Config.KEY_USER_ID);
        }else{
            finish();
        }
        geoPoint = BmobUser.getCurrentUser(this,User.class).getLocation();

    }

    @Override
    protected void initWidget() {
        super.initWidget();
        nearby_list = (XListView) findViewById(R.id.nearby_list);
        hullSwipe = (SwipeRefreshLayout) findViewById(R.id.hullSwipe);

    }

    @Override
    protected void initWidgetListener() {
        super.initWidgetListener();
        hullSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                getNearByList(true);
            }
        });

        nearby_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle;
                bundle = new Bundle();
                Dynamic Dynamic = (Dynamic) nearByAdapter.getItem(position);
                bundle.putString(Constants.DYNAMIC_ID, Dynamic.getObjectId());
                IntentUtil.start_activity_for_result(UserDynamicListActivity.this, DynamicDetailActivity.class, MainActivity.MSG_REFRESH_DYNAMIC_CODE, bundle);
            }
        });
    }

    @Override
    protected void initWidgetState() {
        super.initWidgetState();
        hullSwipe.setColorSchemeResources(R.color.btn_blue, R.color.vip_male);
        hullSwipe.setRefreshing(true);
        nearby_list.setPullLoadEnable(true);
        nearby_list.setXListViewListener(this);

        nearby_list.setAdapter(nearByAdapter);
        getNearByList(true);
    }

    @Override
    public void onClick(View v) {

    }

    int page = 0;
    int limit = 20;

    private void getNearByList(final boolean isRefresh) {
        BmobQuery<Dynamic> query = new BmobQuery<Dynamic>();
        query.addWhereEqualTo("authorId",userId);
        query.setLimit(limit);
        query.setSkip(limit * page);
        query.include("author,images");
        query.order("-createdAt");
        query.findObjects(UserDynamicListActivity.this, new FindListener<Dynamic>() {
            @Override
            public void onSuccess(List<Dynamic> Dynamics) {
                dynamicList = Dynamics;
                refreshLoad();
                resetBottom(Dynamics,isRefresh);

                if (Dynamics != null && Dynamics.size() != 0) {
                    LatLng ll;
                    for (Dynamic info : dynamicList) {
                        BmobGeoPoint point = info.getLocation();
                        if (point == null) continue;
                        ll = new LatLng(point.getLatitude(), point.getLongitude());
                        int meter = (int) DistanceUtil.getDistance(ll, new LatLng(geoPoint.getLatitude(), geoPoint.getLongitude()));
                        info.setMeter(meter);
                    }

                    if (isRefresh) {
                        nearByAdapter.setList(Dynamics);
                    } else {
                        nearByAdapter.addAll(Dynamics);
                    }

                    page++;
                }
            }

            @Override
            public void onFinish() {
                hullSwipe.setRefreshing(false);
            }

            @Override
            public void onError(int i, String s) {
                ShowToast(s);
                refreshLoad();
            }
        });
    }

    private void refreshLoad() {
        nearby_list.stopLoadMore();
        nearby_list.stopRefresh();
    }
    /**
     * 设置listview底部显示方式
     */
    private void resetBottom(List<Dynamic> list,boolean isRefresh){
        if (list != null&&!list.isEmpty()) {
            if (list.size()<limit) {
                nearby_list.setPullLoadEnable(false);
            }
        }else{
            if (isRefresh){
                nearby_list.setFooterText("暂无动态");
            }else{
                nearby_list.setPullLoadEnable(false);
            }

        }
    }
    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
        page=0;
        getNearByList(true);
    }

    @Override
    public void onLoadMore() {
        getNearByList(false);
    }

}
